package demo4;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: fzykd
 * Date: 2022-05-09
 * Time: 20:11
 */

class Student implements Comparable<Student>{//泛型 比较什么 要比较学生了
    public String name;
    public int age;
    public double score;

    public Student(String name, int age, double score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }



    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }

    public int compareTo(Student o){ //比较的是学生 但是学生里面有很多的属性
        //这里面实现的就是 根据学生的什么属性比较
        if (this.age > o.age){ // 谁调用了compareTo谁就是this
            return 1;
        }else if (this.age == o.age){
            return 0;
        }else {
            return -1;
        }
        //return this.age - o.age;
    }
}


public class Test {

    public static void main(String[] args) {//compareTo实现对自定义类型的比较
        // 在重新写的compareTo方法中 指定比较自定义类中的某个属性
        Student student1= new Student("bit",12,98.9);
        Student student2= new Student("haha",18,88.9);
//        if (student1.compareTo(student2)>0){
//
//        }
        System.out.println(student1.compareTo(student2));

    }

    public static void main2(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("bit",12,98.9);
        students[1] = new Student("haha",18,88.9);
        students[2] = new Student("heh",16,78.9);
        System.out.println(Arrays.toString(students));

        Arrays.sort(students);//不知道依据什么来排序 是name 还是age 还是score

        System.out.println(Arrays.toString(students));
    }

    public static void main1(String[] args) {
        int[] array = {1,54,45,78,454,23,34};
        System.out.println(Arrays.toString(array));
        Arrays.sort(array);
        System.out.println(Arrays.toString(array));
    }

}
